// index page
// ============

define(['require', 'zepto', 'utils', 'wxupload', 'sui', 'zeptoFx', 'zeptoTouch'], function(require, $, utils, wxupload) {
  return {
    init: function(data) {
      this.initPage();
      
      this.bindImageUpload(data.jsConfig, data.roundId, data.imgUrl);
    },
    
    initPage: function() {
      var that = this;
      $(document).on("click", ".summary-item .detail-link", function() {
        $(this).parent().find(".description").css("overflow-y", "scroll")
      })
    },
    
    bindImageUpload: function(config, roundId, imgUrl) {
      var that = this;
      wxupload.injectConfig(config)
      
      $(".before .upload-image").on('click', function(e){
        if($(this).hasClass("upload-image")) {
          wxupload.invokeUpload(
              function(localId) {
              },
              
              function(mediaId) {
                that.uploadImage(roundId, mediaId, "before", imgUrl)
              }
            )
          }
      });
      
      $(".before-update").on('click', function(e){
          wxupload.invokeUpload(
              function(localId) {
              },
              
              function(mediaId) {
                that.updateImage(roundId, mediaId, "before", imgUrl)
              }
          )
      });
      
      $(".after .upload-image").on('click', function(e){
        if($(this).hasClass("upload-image")) {
          wxupload.invokeUpload(
            function(localId) {
            },
          
            function(mediaId) {
              that.uploadImage(roundId, mediaId, "after", imgUrl)
            }
          )
        }
      });
      
      $(".after-update").on('click', function(e){
          wxupload.invokeUpload(
              function(localId) {
              },
              
              function(mediaId) {
                that.updateImage(roundId, mediaId, "after", imgUrl)
              }
          )
      });
    },
    
    uploadImage: function(roundId, mediaId, suffix, imgUrl) {
        $.showPreloader();
        
        $.ajax({
            type: 'POST',
            url: __CTX__.root + '/rounds/' + roundId + '/result-report/upload-contrast-photo',
            data: {
              'mediaId': mediaId,
              'suffix': suffix
            }
        }).done(function(response) {
            $.hidePreloader();
            utils.processResult(response, function(result) {
              $.toast('图片上传成功');
              $("."+suffix+" img").attr("src",imgUrl + "/" + result.data.fileName);
              $("."+suffix+" img").attr("alt",suffix == "before" ? "入营时照片" : "结营时照片");
              $("."+suffix+" img").attr("width","100%");
              $("."+suffix+" img").parent().removeClass("upload-image");
              $("."+suffix+" img").parent().css("margin-bottom", "1.5rem");
              $("."+suffix+" img").parent().next().show();
            }, function(result) {
              $.toast(result.message || '图片上传失败')
            })
        })
    },
    
    updateImage: function(roundId, mediaId, suffix, imgUrl) {
        $.showPreloader();
        
        $.ajax({
            type: 'POST',
            url: __CTX__.root + '/rounds/' + roundId + '/result-report/upload-contrast-photo',
            data: {
              'mediaId': mediaId,
              'suffix': suffix
            }
        }).done(function(response) {
            $.hidePreloader();
            utils.processResult(response, function(result) {
              $.toast('图片上传成功');
              $("."+suffix+" img").attr("src",imgUrl + "/" + result.data.fileName);
            }, function(result) {
              $.toast(result.message || '图片上传失败')
            })
        })
    }
  }
})